package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Psych;
import com.hs.service.PsychService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("psych")
public class PsychController {
    @Autowired
    private PsychService psychService;
    @PostMapping
    public R addPsych(@RequestBody Psych psych){
        int i = psychService.addPsych(psych);
        if(i>0){
            return R.ok();
        }
        return R.fail("失败");
    }
    @DeleteMapping("{id}")
    public R deletePsych(@PathVariable("id") int id){
        int i = psychService.deletePsych(id);
        if(i>0){
            return R.ok();
        }
        return R.fail("失败");
    }
    @PutMapping
    public R updatePsych(@RequestBody Psych psych){
        int i = psychService.updatePsych(psych);
        if(i>0){
            return R.ok();
        }
        return R.fail("失败");
    }
    @GetMapping("{id}")
    public R getPsychById(@PathVariable("id") int id){
        Psych psych = psychService.getPsychById(id);
        if(psych!=null){
            return R.ok(psych);
        }
        return R.error("错误");
    }
    @GetMapping("page")
    public R getPsychsByPage(Psych psych,
                             @RequestParam(required = false,defaultValue = "1") int pageNum,
                             @RequestParam(required = false,defaultValue = "2") int pageSize){
        PageInfo<Psych> psychsByPage = psychService.getPsychsByPage(psych, pageNum, pageSize);
        return R.ok(psychsByPage);
    }
    @GetMapping("/alls")
    public R getAllPsychs(){
        List<Psych> psychs = psychService.getAllPsychs();
        return R.ok(psychs);
    }
    @GetMapping("/ById/{id}")
    public R getById(@PathVariable("id") int id){
        Psych psych = psychService.getPsychById(id);
        return R.ok(psych);
    }

}
